const router = require("express").Router();
const mysql = require("../mysql");
const PostCollection = require("../mysql/collection/PostCollection");
const uuid = require("uuid");

/**
 * @api {post} /admin/post-collection/add 添加帖子收藏
 * @apiName AddPostCollection
 * @apiGroup PostCollection
 * 
 * @apiParam {String} postId 帖子ID
 * @apiParam {String} userId 用户ID
 * @apiHeader {String} token 管理员token
 * 
 * @apiSuccess {String} code 状态码
 * @apiSuccess {String} message 描述信息
 * 
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "收藏成功"
 *     }
 */
router.post("/add", (req, res, next) => {
  const { postId, userId } = req.body;
  const collectionData = {
    collectionId: "collection_" + uuid.v4(),
    postId,
    userId
  };
  
  mysql.insert(PostCollection, collectionData).then(() => {
    res.send({
      code: "200",
      message: "收藏成功"
    });
  });
});

/**
 * @api {post} /admin/post-collection/delete 删除帖子收藏
 * @apiName DeletePostCollection
 * @apiGroup PostCollection
 * 
 * @apiParam {String} collectionId 收藏ID
 * @apiHeader {String} token 管理员token
 * 
 * @apiSuccess {String} code 状态码
 * @apiSuccess {String} message 描述信息
 * 
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "取消收藏成功"
 *     }
 */
router.post("/delete", (req, res, next) => {
  const { collectionId } = req.body;
  
  mysql.delete(PostCollection, { collectionId }).then(() => {
    res.send({
      code: "200",
      message: "取消收藏成功"
    });
  });
});

/**
 * @api {get} /admin/post-collection/list 获取用户收藏列表
 * @apiName GetPostCollectionList
 * @apiGroup PostCollection
 * 
 * @apiParam {String} userId 用户ID
 * @apiParam {Number} [pageNum=1] 页码
 * @apiParam {Number} [pageSize=10] 每页数量
 * @apiHeader {String} token 管理员token
 * 
 * @apiSuccess {String} code 状态码
 * @apiSuccess {String} message 描述信息
 * @apiSuccess {Array} data.list 收藏列表数据
 * @apiSuccess {Number} data.total 总记录数
 * @apiSuccess {Number} data.pageNum 当前页码
 * @apiSuccess {Number} data.pageSize 每页数量
 * 
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "获取收藏列表成功",
 *       "data": {
 *         "list": [],
 *         "total": 0,
 *         "pageNum": 1,
 *         "pageSize": 10
 *       }
 *     }
 */
router.get("/list", (req, res, next) => {
  const { userId, pageNum = 1, pageSize = 10 } = req.query;
  
  Promise.all([
    mysql.find(PostCollection, { userId }, { _id: 0, __v: 0 })
      .skip((pageNum - 1) * pageSize)
      .limit(pageSize * 1),
    mysql.count(PostCollection, { userId })
  ]).then(([list, total]) => {
    res.send({
      code: "200",
      message: "获取收藏列表成功",
      data: {
        list,
        total,
        pageNum: pageNum * 1,
        pageSize: pageSize * 1
      }
    });
  });
});

/**
 * @api {get} /admin/post-collection/check 检查是否已收藏
 * @apiName CheckPostCollection
 * @apiGroup PostCollection
 * 
 * @apiParam {String} postId 帖子ID
 * @apiParam {String} userId 用户ID
 * @apiHeader {String} token 管理员token
 * 
 * @apiSuccess {String} code 状态码
 * @apiSuccess {String} message 描述信息
 * @apiSuccess {Boolean} data.isCollected 是否已收藏
 * 
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "code": "200",
 *       "message": "查询成功",
 *       "data": {
 *         "isCollected": true
 *       }
 *     }
 */
router.get("/check", (req, res, next) => {
  const { postId, userId } = req.query;
  
  mysql.find(PostCollection, { postId, userId }).then((data) => {
    res.send({
      code: "200",
      message: "查询成功",
      data: {
        isCollected: data.length > 0
      }
    });
  });
});

module.exports = router;